<template>
  <div class="home-container">
    <div class="hero-section">
      <h1>欢迎使用简易招聘系统</h1>
      <p>连接招聘方与应聘方，让求职更简单</p>
    </div>
    
    <div class="features-section">
      <div class="feature-card" v-if="!isAuthenticatedComputed">
              <h3>招聘方</h3>
              <p>发布职位信息，寻找合适的人才</p>
              <router-link to="/register" class="feature-btn">注册成为招聘方</router-link>
            </div>
            
            <div class="feature-card" v-if="!isAuthenticatedComputed">
              <h3>应聘方</h3>
              <p>浏览职位信息，申请心仪的工作</p>
              <router-link to="/register" class="feature-btn">注册成为应聘方</router-link>
            </div>
            
            <div class="feature-card" v-if="isAuthenticatedComputed && isRecruiterComputed">
              <h3>我的职位</h3>
              <p>管理您发布的职位信息</p>
              <router-link to="/my-jobs" class="feature-btn">查看我的职位</router-link>
            </div>
            
            <div class="feature-card" v-if="isAuthenticatedComputed && isApplicantComputed">
              <h3>职位列表</h3>
              <p>浏览并申请感兴趣的职位</p>
              <router-link to="/jobs" class="feature-btn">查看职位列表</router-link>
            </div>
    </div>
    
    <!-- 职位列表部分 -->
    <div class="job-list-section">
      <div class="section-header">
              <h2>最新职位</h2>
              <router-link to="/jobs" class="view-all-link" v-if="isAuthenticatedComputed && isApplicantComputed">查看全部职位</router-link>
              <router-link to="/register" class="view-all-link" v-else>注册查看全部职位</router-link>
            </div>
      
      <div v-if="loading" class="loading">
        加载中...
      </div>
      
      <div v-else-if="jobs.length === 0" class="empty-state">
        <p>暂无职位信息。</p>
      </div>
      
      <div v-else class="jobs-list">
        <div v-for="job in displayedJobs" :key="job.id" class="job-card">
          <div class="job-header">
            <h3>{{ job.title }}</h3>
          </div>
          
          <div class="job-details">
            <p><strong>公司:</strong> {{ job.company }}</p>
            <p><strong>地点:</strong> {{ job.location }}</p>
            <p><strong>薪资:</strong> {{ job.salary || '面议' }}</p>
            <p><strong>发布时间:</strong> {{ formatDate(job.createdAt) }}</p>
          </div>
          
          <div class="job-description">
            <p>{{ truncateDescription(job.description) }}</p>
          </div>
          
          <div class="job-actions">
            <router-link :to="`/jobs/${job.id}`" class="view-details-btn">查看详情</router-link>
          </div>
        </div>
      </div>
    </div>
    
    <div class="stats-section" v-if="isAuthenticatedComputed">
          <div class="stat-card">
            <h3>{{ jobCount }}</h3>
            <p>职位总数</p>
          </div>
          <div class="stat-card">
            <h3>{{ applicationCount }}</h3>
            <p>申请总数</p>
          </div>
        </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { isAuthenticated, isRecruiter, isApplicant } from '../services/authService';
import { getAllJobs } from '../services/jobService';
import { getAllApplications } from '../services/applicationService';

// 数据
const jobCount = ref(0);
const applicationCount = ref(0);
const jobs = ref([]);
const loading = ref(true);

// 计算属性
const isAuthenticatedComputed = computed(() => isAuthenticated());
const isRecruiterComputed = computed(() => isRecruiter());
const isApplicantComputed = computed(() => isApplicant());

const currentUserRole = computed(() => {
  if (isRecruiterComputed.value) return 'recruiter';
  if (isApplicantComputed.value) return 'applicant';
  return null;
});

// 显示的职位列表（限制数量）
const displayedJobs = computed(() => {
  return jobs.value.slice(0, 5); // 只显示前5个职位
});

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString);
  return date.toLocaleDateString('zh-CN');
};

// 截断职位描述
const truncateDescription = (description) => {
  if (description.length > 100) {
    return description.substring(0, 100) + '...';
  }
  return description;
};

// 加载职位数据
const loadJobs = () => {
  try {
    jobs.value = getAllJobs();
  } catch (err) {
    console.error('加载职位失败:', err);
  } finally {
    loading.value = false;
  }
};

// 加载统计数据
const loadStats = () => {
  try {
    jobCount.value = getAllJobs().length;
    applicationCount.value = getAllApplications().length;
  } catch (err) {
    console.error('加载统计数据失败:', err);
  }
};

// 组件挂载时加载数据
onMounted(() => {
  loadJobs();
  if (isAuthenticated()) {
    loadStats();
  }
});
</script>

<style scoped>
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.hero-section {
  text-align: center;
  padding: 3rem 1rem;
  background: linear-gradient(135deg, #3498db, #2c3e50);
  color: white;
  border-radius: 8px;
  margin-bottom: 2rem;
}

.hero-section h1 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.hero-section p {
  font-size: 1.2rem;
  opacity: 0.9;
}

.features-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  margin-bottom: 2rem;
}

.feature-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 2rem;
  text-align: center;
}

.feature-card h3 {
  margin-top: 0;
  color: #2c3e50;
}

.feature-card p {
  color: #34495e;
  margin-bottom: 1.5rem;
}

.feature-btn {
  padding: 0.75rem 1.5rem;
  background-color: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.feature-btn:hover {
  background-color: #2980b9;
}

.stats-section {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 2rem;
}

.stat-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 2rem;
  text-align: center;
}

.stat-card h3 {
  font-size: 2rem;
  margin: 0 0 0.5rem 0;
  color: #3498db;
}

.stat-card p {
  margin: 0;
  color: #34495e;
  font-weight: 500;
}

.job-list-section {
  margin: 3rem 0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.section-header h2 {
  margin: 0;
  color: #2c3e50;
}

.view-all-link {
  color: #3498db;
  text-decoration: none;
  font-weight: 500;
}

.view-all-link:hover {
  text-decoration: underline;
}

.loading,
.empty-state {
  text-align: center;
  padding: 2rem;
  font-size: 1.2rem;
  color: #7f8c8d;
}

.jobs-list {
  display: grid;
  gap: 1.5rem;
}

.job-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 1.5rem;
}

.job-header {
  margin-bottom: 1rem;
}

.job-header h3 {
  margin: 0;
  color: #2c3e50;
}

.job-details {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.job-details p {
  margin: 0;
  color: #34495e;
}

.job-description p {
  margin: 0 0 1rem 0;
  color: #34495e;
  line-height: 1.5;
}

.job-actions {
  text-align: right;
}

.view-details-btn {
  padding: 0.5rem 1rem;
  background-color: #3498db;
  color: white;
  text-decoration: none;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.view-details-btn:hover {
  background-color: #2980b9;
}

@media (max-width: 768px) {
  .hero-section h1 {
    font-size: 2rem;
  }
  
  .hero-section p {
    font-size: 1rem;
  }
  
  .features-section {
    grid-template-columns: 1fr;
  }
  
  .section-header {
    flex-direction: column;
    gap: 1rem;
  }
  
  .job-details {
    grid-template-columns: 1fr;
  }
}
</style>